package cn.itcast.hotel.service.impl;

import cn.itcast.hotel.mapper.HotelMapper;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.UserBean;
import cn.itcast.hotel.service.IHotelService;
import cn.itcast.hotel.util.SendCode;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@Service
@SuppressWarnings("all")
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {

    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param user
     * @param session
     * @return
     */
    @Override
    public String login(UserBean user, HttpSession session,String userPhone) {

        if(user.getUserPhone()!=null && user.getUserPhone()!=""){

            UserBean use = hotelMapper.findUserByPhone(userPhone);

            if(use==null){
                return "手机号不存在,请先注册";
            }
            String key = "code_"+userPhone;
            if (redisTemplate.hasKey(key)){
                return "验证码已经发送过了";
            }
            JSONObject obj = SendCode.sendCode(userPhone);
            int code = obj.getIntValue("code");
            if(code!=200){
                return "发送短信失败!!!";
            }

            String round = obj.getString("obj");
            redisTemplate.opsForValue().set(key,round,60, TimeUnit.MINUTES);
            return "发送成功!!!";
        }

        UserBean user2 = hotelMapper.findUserName(user.getUsername());
            if (user2==null) {
                return "账号错误！！！";
            }
            if (!user2.getPassword().equals(user.getPassword())) {
                return "密码错误！！！";
            }
            session.setAttribute("user", user2);

            return "登录成功";
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @Override
    public String register(UserBean user) {
        UserBean user2 = hotelMapper.findUserName(user.getUsername());
        if (user2!=null){
            return "账号已存在!!!";
        }else {
            hotelMapper.registerUser(user);
        }
        return "注册成功";
    }

    /**
     * 修改密码
     * @param userId
     * @return
     */
    @Override
    public UserBean changePassword(Integer userId) {

        return hotelMapper.changePassword(userId);
    }

//    /**
//     * 短信验证登录
//     * @param userPhone
//     * @return
//     */
//    @Override
//    public String sendCode(String userPhone) {
//
//        UserBean user = hotelMapper.findUserByPhone(userPhone);
//        if(user==null){
//            return "手机号不存在,请先注册";
//        }
//        String key = "code_"+userPhone;
//        if (redisTemplate.hasKey(key)){
//            return "验证码已经发送过了";
//        }
//        JSONObject obj = SendCode.sendCode(userPhone);
//        int code = obj.getIntValue("code");
//        if(code!=200){
//            return "发送短信失败!!!";
//        }
//        String round = obj.getString("obj");
//        redisTemplate.opsForValue().set(key,round,60, TimeUnit.MINUTES);
//        return "发送成功!!!";
//    }
}
